#include "system.h"
#include "struct.h"
#include "render.h"

#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#include <jni.h>
#include <android/log.h>
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>

extern char g_resPath[300];
extern int g_resolution_x, g_resolution_y;

AAssetManager* globalAassetMgr;
JNIEnv* globalJavaEnv;
jobject globalJNIInterface;

int g_android_touched;
ufoPoint g_android_touched_point;

int internal_route_sizeChanged(void* wParam, void* lParam);
void ufoSetupAppInfo(UFOAPP_INFO *info);

int internal_route_touchMove(void* wParam, void* lParam);
int internal_route_touchDown(void* wParam, void* lParam);
int internal_route_touchUp(void* wParam, void* lParam);

const char* ufoGetResPath()
{
	//strcpy(g_resPath, "/storage/sdcard0/res/");
	//strcpy(g_resPath, "/mnt/sdcard/res/");
	strcpy(g_resPath, "res/");
	return g_resPath;
}

int ufoGetTouchPos(float *x, float *y)
{
	*x = g_android_touched_point.x;
	*y = g_android_touched_point.y;
	*x /= ufoGetDPI();
    *y /= ufoGetDPI();
	return 1;
}

int ufoGetTouchState()
{
	return g_android_touched;
}

int ufoLog(const char* tag, const char *fmt, ...)
{
	va_list	arglist;
	char buf[2048] = {0};
	char pszfinalbuf[2048] = {0};
	int len = 0;

	va_start(arglist, fmt);

	len = vsnprintf(buf,sizeof(buf)-1, fmt, arglist);

	va_end(arglist);
	if (len >= 2048)
		buf[2047] = '\0'; 
		
	snprintf(pszfinalbuf, sizeof(buf)-1, "%s: %s\n", tag, buf);
	__android_log_print(ANDROID_LOG_INFO,tag,buf);
	
	return 1;
}

float ufoGetDPI()
{
	if (g_resolution_x == 480)
		return 1.5f;
		
	if (g_resolution_x == 720)
		return 2.25f;	
		
	if (g_resolution_x == 640)
		return 2.0f;

	if (g_resolution_x == 540)
		return 1.6875f;
		
	return 1.0f;
}

int ufoGetResolution(int *width, int *height)
{
	*width = g_resolution_x;
	*height = g_resolution_y;
}

//################

JNIEXPORT void JNICALL Java_com_ufo_GLThread_ufoRuningLoop(JNIEnv * env, jobject obj,  jint param)
{
    ufoRuningLoop((void*)param);
}

JNIEXPORT void JNICALL Java_com_ufo_GLThread_onSizeChanged(JNIEnv * env, jobject obj,  jint width, jint height)
{
    ufoRect rect = {0, 0, width, height};
	ufoLog("sizeChanged", "size = %d %d", width, height);
	internal_route_sizeChanged(&rect, 0);
}

JNIEXPORT void JNICALL Java_com_ufo_GLThread_ufoSetupAppInfo(JNIEnv * env, jobject obj, jobject JNIInterface)
{
	globalJavaEnv = env;
	globalJNIInterface = (*env)->NewGlobalRef(env, JNIInterface);
	if (globalJNIInterface != 0)
		ufoLog("globalJNIInterface", "globalJNIInterface != 0");
		
	UFOAPP_INFO info = {0};
	ufoLog("ufoInit", "ufoInit");
	ufoSetupAppInfo(&info);
}

JNIEXPORT void JNICALL Java_com_ufo_GLThread_onTouchDown(JNIEnv * env, jobject obj,  jfloat x, jfloat y)
{
	ufoPoint pt = {x, y};
	g_android_touched = 1;
	g_android_touched_point = pt;
	internal_route_touchDown(&pt, 0);
}

JNIEXPORT void JNICALL Java_com_ufo_GLThread_onTouchUp(JNIEnv * env, jobject obj,  jfloat x, jfloat y)
{
	ufoPoint pt = {x, y};
	g_android_touched = 0;
	g_android_touched_point = pt;
	internal_route_touchUp(&pt, 0);
}

JNIEXPORT void JNICALL Java_com_ufo_GLThread_onTouchMove(JNIEnv * env, jobject obj,  jfloat x, jfloat y)
{
	ufoPoint pt = {x, y};
	g_android_touched_point = pt;
	internal_route_touchMove(&pt, 0);
}

JNIEXPORT float JNICALL Java_com_ufo_GLThread_ufoGetDPI(JNIEnv * env, jobject obj)
{
	return ufoGetDPI();
}

JNIEXPORT void JNICALL Java_com_ufo_GLThread_ufoAndroidSetAsset(JNIEnv * env, jobject obj, jobject assetManager)
{
	ufoLog("ufoAndroidSetAsset", "ufoAndroidSetAsset");
	globalAassetMgr = AAssetManager_fromJava(env, assetManager);
}

JNIEXPORT void JNICALL Java_com_ufo_GLThread_ufoAndroidSetResolution(JNIEnv * env, jobject obj, jint width, jint height)
{
	g_resolution_x = width;
	g_resolution_y = height;
}